const _request = require("../../utils/request.js");
const app = getApp()
Page({
  data: {
    mallimgUrl: app.globalData.mallimgUrl,
    search:'',
    typeActive: 1,//1综合 2销量 3筛选唤起弹窗
    activelist:[
      {
        min:0,
        max:100,
      },{
        min:100,
        max:300,
      },{
        min:300,
        max:500,
      },{
        min:500,
        max:1000,
      }
    ],
    active4:false,//筛选弹窗
    active3:-1,//价格 1从高到低，2从低到高
    active2:-1,//价格区间下标

    toolbarList: [1, 2, 3, 4, 5, 6],
    searchHistoryList: [],
    filterFlag: false,
    queryText: '',
    inputActive: 0,
    goodslist: [],
    showHistoryFlag: true,
    showDelFlag: 0,
    typeActive: 1,
    moneyConfigList: ['0-1000', '1000-3000', '3000-5000', '5000-10000'],
    moneyActive: '',
    minMoney: '',
    maxMoney: ''
  },
  onLoad: function (options) {
    this.querygoods()
  },
  // 获取搜索
  inputsearch(e){
    this.setData({
      search:e.detail.value
    })
  },
  // 条件查询
  switchType(e) {
    this.setData({ typeActive: e.currentTarget.dataset.id })
    if (e.currentTarget.dataset.id == 3) { //筛选
      this.setData({
        active4:true
      })
    }else{ //排序
      this.setData({
        active4:false,
        active3:-1,
        active2:-1,
      })
      this.querygoods()
    }
  },
  // 搜索
  gosearch(){
    this.setData({
      typeActive:1,
      active4:false,
      active3:-1,
      active2:-1,
    })
    this.querygoods()
  },
  // 价格排序
  opgaodi(e){
    this.setData({
      active3:e.currentTarget.dataset.index
    })
  },
  // 价格区间
  bindtapscreen(e){
    if(this.data.active2==e.currentTarget.dataset.index){
      this.setData({
        active2:0
      })
      return
    }
    this.setData({
      active2:e.currentTarget.dataset.index
    })
  },
  // 重置
  chongzhifun(){
    this.setData({
      active3:-1,
      active2:-1,
    })
  },
  // 完成筛选
  closemak(){
    if(this.data.active3<0 && this.data.active2<0){
      this.setData({
        typeActive:1,
      })
    }
    this.querygoods()
    this.setData({
      active4:false
    })
  },
  // 商品列表
  querygoods() {
    this.setData({
      goodslist: [],
      showHistoryFlag: false,
    })
    _request.postObj('/commodity/getallcommodityinfo', {
      pageNo:1,
      pageSize:1000,
      name: this.data.search,
      sort:this.data.typeActive==1?'':(this.data.typeActive==2?'sale':'price'),//sale：销量，price：价格
      order:this.data.active3==2?'asc':(this.data.active3==1?'desc':''),//asc：升序，desc：降序
      minPrice:(this.data.typeActive==3&&this.data.active2>0)?this.data.activelist[this.data.active2].min:'',
      maxPrice:(this.data.typeActive==3&&this.data.active2>0)?this.data.activelist[this.data.active2].max:'',
    }, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        this.setData({
          goodslist: res.data.data.rows || []
        })
      }
    })
  },
  // 跳转
  gotoNext: function(item) {
    wx.navigateTo({
      url: '/pages/shopingmall/goodsdetail' + this.getparams(item.currentTarget.dataset)
    })
  },
  getparams: function(item) {
    const list = []
    for(const key in item) {
      if (key === 'url') {
        continue;
      }
      list.push(key + '=' + item[key])
    }
    if (list.length) {
      return '?' + list.join('&')
    } else {
      return '';
    }
  }
})